package peer;

import java.io.Serializable;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashSet;

import net.jini.core.entry.Entry;

public class Message<T>  implements Serializable, Entry {
	//public static enum Types {ping, retreiveJob, getConnections};
	public static final String DELIM = ";";
	public String id;
	public String protocol;
	public String type;
	public T data;
	
	public Message() { 
		
	}
	
	@SuppressWarnings("unchecked")
	public Message(DatagramPacket packet) {
		String data = new String(packet.getData());	
		String[] contents = data.split(DELIM);
		//for(String e : contents) { System.out.println(e);}
		id = contents[0];
		protocol = contents[1];
		type = contents[2];
		
	}
	
	public void toPacket(DatagramPacket packet) {
		this.id = Peer.id;
		byte[] buffer = (id + DELIM + protocol + DELIM + type).getBytes();
		packet.setData(buffer);
		
	}
	
	public String toString() { return id+DELIM+protocol +DELIM + type + DELIM + data; } 
}
